% write_basic_term_latex.pro

% intended for inclusion in module write_basic_term

:- style_check(-atom).          % allow long atoms


%------------------------------------------------------------------------------
% WRITE A BASIC TERM IN AS latex term in math mode FORMAT
%------------------------------------------------------------------------------

%   write_basic_term_latex(+Term)
%
%   write plain text describing term
%   Term is a basic term represented as a typed prolog term
%
write_basic_term_latex(Term) :-
    basic_term_latex(Term).



basic_term_latex(X::_T) :-
    X =.. [abstraction|Ps],
    !,
    findall(
        A,
        (
         member(P-_,Ps),
         with_output_to(atom(A), basic_term_latex(P))
        ),
        As
    ),
    concat_atom(As,', ',S),
    %
    memberchk(_-M,Ps),
    abstraction_brackets(M, LeftBracket,RightBracket),
    format('\\~w ~w \\~w', [LeftBracket,S,RightBracket]).

basic_term_latex(X::_T) :-
    X =.. [tuple|Ps],
    !,
    findall(
        A,
        (
         member(P,Ps),
         with_output_to(atom(A), basic_term_latex(P))
        ),
        As
    ),
    concat_atom(As,',',S),
    format('(~w)', [S]).

basic_term_latex(X::_T) :-
    X =.. [_C|Ps],
    !,
    % constructor
    term_to_latex(X,Y),
    format(Y),
    %
    (Ps == []
    ->  true
    ;
        findall(
            A,
            (
             member(P,Ps),
             with_output_to(atom(A), basic_term_latex(P))
            ),
            As
        ),
        concat_atom(As,',',S),
        format('~w', [S])
    ).


term_to_latex(true, '\\top') :- !.
term_to_latex(false, '\\bot') :- !.
term_to_latex(X, X) :-
    number(X), !.
term_to_latex(X, Y) :-
    atom_chars(X, [H|T]),
    char_type(H, alpha), !,
    upcase_atom(H, U),
    atom_chars(UX, [U|T]),
    concat_atom(['\\textit{',UX,'}'], Y).
term_to_latex(X, X).
